package com.gzc.just.play.last.war.centralserver.quest.repository;

import com.gzc.just.play.last.war.centralserver.quest.model.QuestProgress;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface QuestProgressRepository extends JpaRepository<QuestProgress, Long> {
    
    List<QuestProgress> findByPlayerId(Long playerId);
    
    Optional<QuestProgress> findByPlayerIdAndQuestId(Long playerId, Long questId);
    
    List<QuestProgress> findByPlayerIdAndStatus(Long playerId, QuestProgress.Status status);
    
    List<QuestProgress> findByQuestId(Long questId);
    
    @Modifying
    @Query("UPDATE QuestProgress qp SET qp.progress = :progress WHERE qp.playerId = :playerId AND qp.questId = :questId")
    void updateProgress(@Param("playerId") Long playerId, @Param("questId") Long questId, @Param("progress") Integer progress);
    
    @Modifying
    @Query("UPDATE QuestProgress qp SET qp.status = :status WHERE qp.playerId = :playerId AND qp.questId = :questId")
    void updateStatus(@Param("playerId") Long playerId, @Param("questId") Long questId, @Param("status") QuestProgress.Status status);
    
    @Modifying
    @Query("DELETE FROM QuestProgress qp WHERE qp.playerId = :playerId")
    void deleteByPlayerId(@Param("playerId") Long playerId);
    
    @Query("SELECT COUNT(qp) FROM QuestProgress qp WHERE qp.playerId = :playerId AND qp.status = 'COMPLETED'")
    Long countCompletedQuestsByPlayerId(@Param("playerId") Long playerId);
}